package com.codingfly.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.codingfly.controller.common.BaseController;
import com.codingfly.core.exception.BizException;
import com.codingfly.core.util.AssertUtils;
import com.codingfly.entity.SysGroup;
import com.codingfly.entity.SysUser;
import com.codingfly.model.Result;
import com.codingfly.model.dto.UserDto;
import com.codingfly.service.SysGroupService;
import com.codingfly.service.SysUserService;
import com.codingfly.service.impl.CommonServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(GroupController.class);
    @Autowired
    private SysGroupService sysGroupService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private CommonServiceImpl commonServiceImpl;
    @Value("{user.default.password:c302123456}")
    private String userDefaultPassword;

    @PostMapping
    public Result add(@RequestBody UserDto dto) {
        checkParams(dto, "username", "groupId", "auditTime");
        SysGroup sysGroup = Optional.ofNullable(sysGroupService.findById(dto.getGroupId())).orElseThrow(()->new BizException("组织机构不存在或者已删除"));
        AssertUtils.isTrue(new Integer(2).equals(sysGroup.getType()), "用户必须挂载到部门下面");
        AssertUtils.isTrue(sysUserService.findByUsername(dto.getUsername())==null, "该账号已存在");
        AssertUtils.isTrue(sysUserService.findByUsername(dto.getUsername())==null, "该账号已存在");
        SysUser newUser = new SysUser();
        newUser.setUsername(dto.getUsername());
        newUser.setGroupId(dto.getGroupId());
        newUser.setAddress(dto.getAddress());
        newUser.setAuditTime(dto.getAuditTime());
        newUser.setMobilePhone(dto.getMobilePhone());
        newUser.setOrderNo(commonServiceImpl.queryNextOrderNoInt("sys_user"));
        newUser.setPassword(BCrypt.hashpw(userDefaultPassword));
        sysUserService.save(newUser);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody UserDto dto) {
        SysUser user = Optional.ofNullable(sysUserService.findById(id)).orElseThrow(()->new BizException("该用户不存在或者已删除"));
        user.setAddress(dto.getAddress());
        user.setMobilePhone(dto.getMobilePhone());
        sysUserService.save(user);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        sysUserService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result update(@PathVariable("id") Integer id) {
        return Result.success(sysUserService.findById(id));
    }

    @PatchMapping("/move/{id}")
    public Result move(@PathVariable("id") Integer id, @RequestParam("action") String action) {
        sysUserService.move(id, action);
        return Result.success();
    }

}
